﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ArmoryPaper : MonoBehaviour
{
    public WeaponData m_SelectedWeapon;

    public List<WpnCategorize> m_WeaponGroupList = new List<WpnCategorize>(); // 같은무기종류별로 묶어주는 리스트

    // Use this for initialization
    void Start()
    {
        m_SelectedWeapon = null;
        GetComponent<UISprite>().depth = transform.parent.GetComponent<UISprite>().depth + 3;

        for (int i = 0; i < transform.childCount; ++i)
        {
            if(transform.GetChild(i).gameObject == GameObject.Find("ArmoryBackPaper").gameObject)
                transform.GetChild(i).GetComponent<UISprite>().depth = GetComponent<UISprite>().depth - 2;
            else if (transform.GetChild(i).GetComponent<UISprite>())
                transform.GetChild(i).GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 1;
            else if (transform.GetChild(i).GetComponent<UILabel>())
                transform.GetChild(i).GetComponent<UILabel>().depth = GetComponent<UISprite>().depth + 1;
        }

        SceneWpnCategorize();
        transform.Find("ScrollBar").GetComponent<ArmoryScrollBar>().ScrollBarReset();
    }

    void OnEnable()
    {
        SceneWpnCategorize();
        m_SelectedWeapon = null;
    }
    // Update is called once per frame
    void Update()
    {
        if (m_SelectedWeapon != null)
        {
            transform.FindChild("ObjectName").GetComponent<UILabel>().text = m_SelectedWeapon.m_strWeaponName;
            //transform.FindChild("ObjectInform").GetComponent<UILabel>().text = m_SelectedWeapon.m_strWeaponInform;
            transform.FindChild("ObjectStat").GetComponent<UILabel>().text = string.Format("{0}\n{1}\n{2}\n{3}", m_SelectedWeapon.m_iWeaponDmg, m_SelectedWeapon.m_iWeaponAccur, m_SelectedWeapon.m_iWeaponRate, m_SelectedWeapon.m_iFullAmmo);

            transform.FindChild("ObjectImg").gameObject.SetActive(true);
            transform.FindChild("ObjectImg").GetComponent<UISprite>().spriteName = m_SelectedWeapon.m_strWeaponName;
            if (m_SelectedWeapon.m_WeaponType == WEAPON_TYPE.RIFLE)
                transform.FindChild("ObjectImg").transform.rotation = Quaternion.AngleAxis(-45.0f, Vector3.forward);
            else
                transform.FindChild("ObjectImg").transform.rotation = Quaternion.AngleAxis(-6.5f, Vector3.forward);
        }
        else
        {
            transform.FindChild("ObjectImg").gameObject.SetActive(false);
            transform.FindChild("ObjectName").GetComponent<UILabel>().text = "";
            transform.FindChild("ObjectStat").GetComponent<UILabel>().text = "";
            transform.FindChild("ScrollObjects").FindChild("SelectedArm").GetComponent<UISprite>().enabled = false;
        }
        transform.FindChild("Date").GetComponent<UILabel>().text = GameObject.Find("Clock").GetComponent<UILabel>().text.Substring(0,12);


    }


    public void SceneWpnCategorize()
    {

        for (int i = 0; i < GameObject.Find("LabelLists").transform.childCount; ++i)
            Destroy(GameObject.Find("LabelLists").transform.GetChild(i).gameObject);

        m_WeaponGroupList.Clear();



        bool bRepeatedCheck;  // 겹치는 무기있는지 검사해줌
        int iTempIndex; // 겹치는곳 인덱스

        List<WeaponData> m_WpnDataList = GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().ArmoryWeaponList;
        for (int i = 0; i < m_WpnDataList.Count; ++i)
        {
            bRepeatedCheck = false;
            iTempIndex = 0;

            for (int j = 0; j < m_WeaponGroupList.Count; ++j)
            {
                if (m_WeaponGroupList[j].m_WpnData.m_WeaponType == m_WpnDataList[i].m_WeaponType &&
                    m_WeaponGroupList[j].m_WpnData.m_strWeaponName == m_WpnDataList[i].m_strWeaponName)
                {
                    bRepeatedCheck = true;
                    iTempIndex = j;
                }
            }

            if (!bRepeatedCheck)  // 새로운종류 무기 등록
            {
                WpnCategorize backWpnData = new WpnCategorize();
                backWpnData.m_WpnData = GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().ArmoryWeaponList[i];
                backWpnData.m_iNumberOfWpn = 1;

                m_WeaponGroupList.Add(backWpnData);
            }
            else  // 기존 무기에 갯수 ++
            {
                ++m_WeaponGroupList[iTempIndex].m_iNumberOfWpn;
            }
        }

        for (int i = 0; i < m_WeaponGroupList.Count; ++i)
        {
            (Instantiate(Resources.Load("Prefaps/UI/Paper/Armory/Label") as GameObject) as GameObject).transform.parent = transform.FindChild("ScrollObjects").FindChild("LabelLists").transform;
            transform.FindChild("ScrollObjects").FindChild("LabelLists").GetChild(transform.FindChild("ScrollObjects").FindChild("LabelLists").childCount-1).GetComponent<ArmoryLabel>().m_iIndex = i;
        }

        transform.Find("ListCount").GetComponent<UILabel>().text = string.Format("{0} / 30", GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().ArmoryWeaponList.Count);
    }

}

[System.Serializable]
public class WpnCategorize 
{
    public WeaponData m_WpnData;
    public int m_iNumberOfWpn;
}